package com.dave.admin.modular.boss.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.dave.admin.common.result.CommonResult;
import com.dave.admin.component.log.SysLog;
import com.dave.admin.modular.boss.dto.MenuNode;
import com.dave.admin.modular.boss.dto.PermissionVo;
import com.dave.admin.modular.boss.entity.Permission;
import com.dave.admin.util.QueryWrapperUtil;
import com.dave.admin.util.SecurityUtil;
import com.dave.admin.modular.boss.service.IPermissionService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜单管理
 *
 * @author Dave
 */
@RestController
@AllArgsConstructor
@RequestMapping("/boss/menu")
public class BossMenuController extends BaseController {

    private final IPermissionService iPermissionService;

    @Override
    public void setPermissionPrefix() {
        super.permissionPrefix = "BossMenu";
    }

    /**
     * 菜单表格树列表
     */
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    @PreAuthorize("hasPermission(#this.getThis(),'')")
    public CommonResult treeList(Permission permission) {
        return CommonResult.success(iPermissionService.treeList(QueryWrapperUtil.getQueryWrapper(permission)));
    }

    /**
     * 菜单列表：当前所属用户的菜单列表
     */
    @RequestMapping(value = "/menuList", method = RequestMethod.GET)
    public CommonResult menuList() {
        //所有权限值
        List<String> permissions = SecurityUtil.getPermissionList().stream().map(x -> x.getValue()).collect(Collectors.toList());
        //所有菜单列表值
        List<MenuNode> menuNodes = iPermissionService.menuList();

        Map result = MapUtil.builder()
                .put("menus", menuNodes)
                .put("perms", permissions).build();
        return CommonResult.success(result);
    }


    /**
     * 获取所有目录的数据
     */
    @RequestMapping(value = "/parentList", method = RequestMethod.GET)
    public CommonResult parentList(@RequestParam(required = false) String name) {
        return CommonResult.success(iPermissionService.parentList(name));
    }


    /**
     * @param permissionVo 入参
     * @return Results 响应数据
     */
    @SysLog("保存菜单")
    @PostMapping
    @PreAuthorize("hasPermission(#this.getThis(),'-Add')")
    public CommonResult save(@RequestBody PermissionVo permissionVo) {
        iPermissionService.save(permissionVo);
        return CommonResult.success();
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return Results 响应数据
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasPermission(#this.getThis(),'-Detail')")
    public CommonResult getById(@PathVariable("id") String id) {
        Permission permission = iPermissionService.getById(id);
        iPermissionService.afterGetItem(permission);
        return CommonResult.success(permission);
    }

    /**
     * @param permissionVo 入参
     * @return Results 响应数据
     */
    @SysLog("通过ID更新菜单")
    @PutMapping
    @PreAuthorize("hasPermission(#this.getThis(),'-Update')")
    public CommonResult updateById(@RequestBody PermissionVo permissionVo) {
        Permission permission = BeanUtil.toBean(permissionVo, Permission.class);
        return CommonResult.success(iPermissionService.updateById(permission));
    }


    /**
     * 通过id删除 监控
     *
     * @param id id
     * @return Results 响应数据
     */
    @SysLog("通过ID删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasPermission(#this.getThis(),'-Delete')")
    public CommonResult removeById(@PathVariable String id) {
        iPermissionService.beforeDelete(id);
        return CommonResult.success(iPermissionService.removeById(id));
    }

}
